import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:s_k_todo/provider/task_add_provider.dart';
import 'package:s_k_todo/util/log_util.dart';
import '../model/task_model.dart';

class TaskAddPage extends StatefulWidget {
  const TaskAddPage({super.key});

  @override
  State<TaskAddPage> createState() => _TaskAddPageState();
}

class _TaskAddPageState extends State<TaskAddPage> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (_) => TaskAddProvider(), builder: (_, _1) => _TaskAddBody());
  }
}

class _TaskAddBody extends StatelessWidget {
  _TaskAddBody({super.key});

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final TextEditingController _taskNameController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    TaskAddProvider provider = context.watch();
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Form(
        key: _formKey,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 15,
            ),
            Row(
              children: [
                IconButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    icon: Icon(Icons.arrow_back_rounded)),
                SizedBox(
                  width: 10,
                ),
                Text(
                  "Task add",
                  style: Theme.of(context).textTheme.headlineLarge,
                ),
                Spacer(),
                TextButton(
                    onPressed: () async {
                      BuildContext myContext = context;
                      _formKey.currentState?.save();
                      TaskEntity? data = await provider
                          .addTask()
                          .onError((error, stackTrace) => null);
                      Navigator.pop(myContext, data);
                    },
                    child: Text("提交"))
              ],
            ),
            SizedBox(
              height: 20,
            ),
            Row(
              children: [
                Text("TaskTitle：",
                    style: Theme.of(context).textTheme.titleSmall),
                Expanded(child: TextFormField(
                  onSaved: (s) {
                    provider.taskName = s ?? "";
                  },
                )),
              ],
            ),
            Row(
              children: [
                Text(
                  "TaskDescription：",
                  style: Theme.of(context).textTheme.titleSmall,
                ),
                Expanded(child: TextFormField(onSaved: (s) {
                  provider.taskTip = s ?? "";
                })),
              ],
            ),
            // ListView.separated(
            //     scrollDirection: Axis.horizontal,
            //     itemBuilder: (ctx, index) => Container(),
            //     separatorBuilder: (ctx, index) => Container(),
            //     itemCount: 10),
            SizedBox(
              height: 10,
            ),
            Wrap(
              spacing: 10,
              runSpacing: 5,
              children: provider.stepList
                  .map((e) => Container(
                        height: 30,
                        decoration: BoxDecoration(
                            color: Theme.of(context).colorScheme.primary,
                            borderRadius: BorderRadius.circular(100)),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            SizedBox(
                              width: 15,
                            ),
                            Text(e,
                                style: const TextStyle(
                                    fontSize: 14, color: Colors.white)),
                            SizedBox(
                              width: 10,
                            ),
                            MouseRegion(
                              cursor: SystemMouseCursors.click,
                              child: GestureDetector(
                                onTap: (){
                                  provider.removeStepByName(e);
                                },
                                child: Container(
                                    height: 30,
                                    width: 30,
                                    decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius:
                                            BorderRadius.circular(100)),
                                    child: Icon(
                                      Icons.close,
                                      size: 16,
                                      color: Colors.red,
                                    )),
                              ),
                            )
                          ],
                        ),
                      ))
                  .toList(),
            ),
            SizedBox(
              height: 20,
            ),
            AnimatedContainer(
              duration: Duration(milliseconds: 200),
              height: provider.addStep ? 100 : 0,
              child: Visibility(
                visible: provider.addStep,
                child: ClipRect(
                  child: SingleChildScrollView(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "添加步骤：",
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                        Row(
                          children: [
                            Text("步骤标题："),
                            Expanded(child: TextFormField(
                              controller: _taskNameController,
                            )),
                            TextButton(
                                onPressed: () {
                                  String text = _taskNameController.value.text;
                                  print(text);
                                  provider.addStepName(text);
                                },
                                child: Text("确定")),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                IconButton(
                    onPressed: () {
                      provider.addStep = !provider.addStep;
                    },
                    icon: const Icon(Icons.filter_list)),
                IconButton(
                    onPressed: () async {
                      DateTime time = DateTime.now();
                      DateTime nextYearDay =
                          DateTime(time.year + 1, time.month, time.day);
                      DateTime? date = await showDatePicker(
                          context: context,
                          firstDate: DateTime.now(),
                          lastDate: nextYearDay);
                      LogUtil.log(date?.toString() ?? "");
                    },
                    icon: const Icon(Icons.calendar_month_rounded)),
                IconButton(
                    onPressed: () {}, icon: const Icon(Icons.star_border))
              ],
            )
          ],
        ),
      ),
    );
  }
}
